package controllers
import (
	"os"
	"io"
	"mime/multipart"
	"reflect"
	cm "ServerGo/common"
)

type EmptyParams struct {}

type ResponseMsg struct {
	No   int `json:"no"`
	Msg  string `json:"msg"`
	Data interface{} `json:"data"`
}

var (
	BUILD_PATH = cm.GetConfigWithString("paths","build_path")
	RECORD_PATH = cm.GetConfigWithString("paths","record_path")
	TMP_PATH_PREFIX = cm.GetConfigWithString("paths","tmp_path")
)

func moveUploadFile(to string, upfile multipart.File) (bool, error) {
	newfile, err := os.OpenFile(to, os.O_WRONLY| os.O_CREATE, 0777)
	defer newfile.Close()

	if err != nil {
		return false, err
	}else {
		if err != nil {
			return false, err
		}else {

			fileBytes := make([]byte, 1024)
			for {
				n, err := upfile.Read(fileBytes)
				if n == 0 || err == io.EOF {
					return true, nil
				}else if err != nil {
					return false, err
				}

				_, err = newfile.Write(fileBytes[:n])
				if err != nil {
					return false, err
				}
			}
		}
	}
}

func getKeys(arr []interface{}) []interface{} {
	tmp := []interface{}{}
	for _, v := range arr {
		if _, ok := v.(map[string]interface{}); ok {
			val := reflect.ValueOf(v)
			kval := reflect.ValueOf("name")
			name := val.MapIndex(kval).Interface()
			tmp = append(tmp, name)
		}
	}
	return tmp
}

func findIndex(val interface{}, arr []interface{}) int {
	index := -1;
	for k, v := range arr {
		if v == val {
			return k
		}
	}
	return index;
}

